package com.wsc.wisdomchina.activitity.introduction.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.InfoDetailBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

/**
 * 街道详情
 */
public class StreetIntroduceFragment extends Fragment implements ContributorView {


    private WebView mWebView;
    private String cidNumber;
    private String FRAG_STREET = "FRAG_STREET";
    private ContributorPresenter contributorPresenter;
    private ProgressBar pb_newsdetail;

    public static StreetIntroduceFragment newInstance(Bundle b) {
        StreetIntroduceFragment selectedOneFragment = new StreetIntroduceFragment();
        selectedOneFragment.setArguments(b);
        return selectedOneFragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contributorPresenter = new ContributorPresenter(this, getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_street_introduce, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    /**
     * 初始化 数据
     */
    private void initData() {
        String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", getActivity());
        if (locationString.length() > 0) {
            StreetInfoListBean streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
            cidNumber = streetInfoListBean.getId();
            contributorPresenter.getStreetInfo(cidNumber, FRAG_STREET);
        }
    }

    /**
     * 初始化控件
     *
     * @param inflate
     */
    private void initView(View inflate) {
        mWebView = ((WebView) inflate.findViewById(R.id.fragment_street_webView));
        //初始化webView 设置
        mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(false);

        pb_newsdetail = (ProgressBar) inflate.findViewById(R.id.pb_newsdetail);
        pb_newsdetail.setVisibility(View.VISIBLE);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pb_newsdetail.setProgress(newProgress);
                pb_newsdetail.postInvalidate();
                if (newProgress == 100) {
                    pb_newsdetail.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                // TODO Auto-generated method stub
                super.onReceivedTitle(view, title);
            }
        });


    }


    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(FRAG_STREET)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object message = respObj.getMessage();
                if (code.equals("200") && message != null) {
                    InfoDetailBean newsDetailBean = JsonTools.jsonStringToObject(JsonTools.objectToJsonString(message), InfoDetailBean.class);
                    String Url = Constant.Head_string + newsDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    mWebView.loadData(Url, "text/html; charset=UTF-8", null);
                } else {
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {

    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }
}
